<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '../dashboard/mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    chartData: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions(data = []) {
      const total = data.reduce((sum, item) => sum + item.value, 0)
      
      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: function(params) {
            const percentage = total > 0 ? ((params.value / total) * 100).toFixed(1) : 0
            return `${params.seriesName}<br/>${params.name}: ${params.value}单 (${percentage}%)`
          }
        },
        legend: {
          orient: 'horizontal',
          bottom: '5%',
          left: 'center',
          itemWidth: 12,
          itemHeight: 12,
          textStyle: {
            fontSize: 12,
            color: '#666'
          }
        },
        color: data.map(item => item.color),
        series: [
          {
            name: '运单状态',
            type: 'pie',
            radius: ['40%', '70%'],
            center: ['50%', '45%'],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 6,
              borderColor: '#fff',
              borderWidth: 2
            },
            label: {
              show: true,
              position: 'outside',
              formatter: function(params) {
                const percentage = total > 0 ? ((params.value / total) * 100).toFixed(1) : 0
                return `${params.name}\n${params.value}单\n${percentage}%`
              },
              fontSize: 11,
              color: '#666'
            },
            emphasis: {
              label: {
                show: true,
                fontSize: '13',
                fontWeight: 'bold'
              },
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            },
            labelLine: {
              smooth: true,
              length: 15,
              length2: 10
            },
            data: data.map(item => ({
              value: item.value,
              name: item.name,
              itemStyle: {
                color: item.color
              }
            })),
            animationType: 'scale',
            animationEasing: 'elasticOut',
            animationDelay: function (idx) {
              return Math.random() * 200
            }
          }
        ]
      })
    }
  }
}
</script>